/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @author: jin Chen 
 * @date: Jun 17, 2003
 * @version: $VERSION$
 */
package edu.psu.geovista.app.plot.axis;

import java.awt.*;
import java.awt.geom.GeneralPath;

public class Thumb extends Rectangle {
    public static final boolean DEBUG=false;
    public static final int EAST=1; //pointer face to east
    public static final int WEST=2;
    public static final int TOP=101; //pointer location type on ruler
    public static final int BTTM=102;
    private int type;//facing type: west or east
    private int locType;// location type: top bottom

    private Color color;
    private Point pt;//the graphic location to where the pointer point
    private String name;
    private float value;


    public Thumb(int type) {
        this.type=type;
        pt=new Point();
        this.setBounds(0,0,20,10); //x,y,w,h
        this.resetLocation();
    }

    public Thumb(int type,Color color,String name) {
        this(type);
        this.color = color;
        this.name =name;
    }
    /*******************************************************************************************************
     *                operation
     *******************************************************************************************************/

     /**
      *  move the pointer to a point on the slider
      *  @param graphic location
      */
     public void moveTo(int loc) {
         if (type==Thumb.EAST ||type==Thumb.WEST ){
             this.setPt(this.getPt().x, loc);//only move y
         }
         else{
             this.setPt(loc, this.getPt().y);//only move y
         }

     }
     /**
      *  move the pointer delta on the slider
      *  @param graphic location
      */
     public void move(int delta) {
         if (type==Thumb.EAST ||type==Thumb.WEST ){
             this.setPt(this.getPt().x, this.getPt().y+delta);//only move y
             //dp{
             if (DEBUG){
                 System.out.println(this.getName() +"Move pt to:"+this.getPt());
                 System.out.println(this.getName() +"Move loc to:"+this.getLocation() );
             }//dp}
         }
         else{
             this.setPt(this.getPt().x+delta, this.getPt().y);//only move y
         }

     }
    /*******************************************************************************************************
     *                GUI
     *******************************************************************************************************/
    public void paintComponent(Graphics g) {
        Graphics2D g2d= (Graphics2D)g;

        //Dimension size=this.getSize() ;
        //Point loc=this.getLocation() ;
        //System.out.println(this.getName() +" loc "+loc);

        Color old=g2d.getColor() ;
        g2d.setColor(color);
        g2d.translate(pt.x,pt.y);
        //System.out.println(this.getName() +" Ploc "+this.getPt() );

        GeneralPath path=this.getShape(this.type );
        if (path!=null){
            g2d.setColor(color);
            g2d.fill(path);
            g2d.setColor(Color.black );
            g2d.draw(path);
        }

        g2d.translate(-pt.x,-pt.y);
        if (this.type ==Thumb.WEST ){
           g2d.drawString(Math.floor(this.getValue()) +" ",this.getLocation().x+this.getSize().width+2,this.getLocation().y);
        }
        else if (this.type ==Thumb.EAST ){
           g2d.drawString(Math.floor( this.getValue()) +" ",this.getLocation().x-30,this.getLocation().y);
        }

        g2d.setColor(old);

    }

    private GeneralPath getShape(int type) {
        GeneralPath path=new GeneralPath(GeneralPath.WIND_EVEN_ODD );//???
        Dimension size=this.getSize() ;
        Point loc=this.getLocation() ;

        switch (type) {
            case EAST:
                path.moveTo(0,0);
                path.lineTo(-size.width/2, size.height/2);
                path.lineTo(-size.width, size.height/2);
                path.lineTo(-size.width, -size.height/2);
                path.lineTo(-size.width/2, -size.height/2);
                path.closePath() ;
                break;
            case WEST:
                path.moveTo(0,0);
                path.lineTo(size.width/2, size.height/2);
                path.lineTo(size.width, size.height/2);
                path.lineTo(size.width, -size.height/2);
                path.lineTo(size.width/2, -size.height/2);
                path.closePath() ;
                break;
        }
        return path;
    }
    /**
     * @param Point the point it point to
     */
    private void resetLocation(){
         Point p=this.getLocation() ;
         Dimension size=this.getSize() ;
         int x, y;
         switch (this.type) {
            case EAST:
                  p.x=pt.x-size.width;
                  p.y=pt.y-size.height/2 ;
                  break;
            case WEST:
                  p.x=pt.x;
                  p.y=pt.y-size.height/2 ;
                  break;
         }
         this.setLocation(p);

         //return p;

    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public Point getPt() {
        return pt;
    }

    public void setPt(Point pt) {
        //System.out.println("setPt"+pt);
        this.pt.setLocation(pt);
        this.resetLocation() ;
    }
    public void setPt(int x, int y) {
        //System.out.println("setPt"+x+" "+y);
        this.pt.x=x;
        this.pt.y=y;
        this.resetLocation() ;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    /**
     *  Only to store the real value, nothing else
     *  Must also move the thumb yourself
     */
    public void setValue(float value) {
        this.value = value;
    }

    public float getValue() {
        return value;
    }

    public int getLocType() {
        return locType;
    }

    public void setLocType(int locType) {
        this.locType = locType;
    }

    public int getType() {
        return type;
    }

    /**
     *  MUST, for MTYAxisUI's map.
     */
    public boolean equals(Object obj){
        String aname=((Thumb)obj).getName() ;
      return this.getName().equals(aname);

    }

    
}
